<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="closeDialog"
    @open="openDialog"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="hAssignRoles">确定</el-button>
      <el-button size="small" @click="closeDialog">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { assignRoles } from '@/api/employees'
import { getUserDetailById } from '@/api/user'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      // 储存选中数据
      roleIds: [],
      list: []
    }
  },
  methods: {
    // 关闭弹层
    closeDialog() {
      this.$emit('close-role-dialog')
      // 清空数据
      this.roleIds = []
    },
    // 弹层打开
    openDialog() {
      this.hGetRoleList()
      this.hGetUserDetailById()
    },
    // 获取数据
    async hGetRoleList() {
      const res = await getRoleList({ page: 1, pagesize: 100 })
      // console.log(res)
      this.list = res.rows
    },
    // 分配角色
    async hAssignRoles() {
      await assignRoles({ id: this.curId, roleIds: this.roleIds })
      // 关闭弹层
      this.closeDialog()
      // 提示用户
      this.$message('角色分配成功')
    },
    // 回显获取
    async hGetUserDetailById() {
      const res = await getUserDetailById(this.curId)
      this.roleIds = res.roleIds
      // console.log(res)
    }
  }
}
</script>
